package com.tanhua.server.service;


import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.common.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class UserService {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${tanhua.sso.url}")
    private String ssourl;
    private  static  final ObjectMapper MAPPER=new ObjectMapper();


    public User queryUserByToken(String token) {
        String url=ssourl+"/user/"+token;
        try {
            String data = restTemplate.getForObject(url, String.class);
            if(StrUtil.isEmpty(data))
                return null;

            User user = MAPPER.readValue(data, User.class);
            log.info(user.toString());
            return user;
        } catch (Exception e) {
           log.error("校验token出错，token="+token,e);
        }
        return null;
    }
}
